<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义属性演示</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        .product-item {
            transition: all 0.3s ease;
        }
        .product-item:hover {
            transform: translateY(-5px);
        }
    </style>
</head>
<body class="bg-gray-100 p-8">
    <div class="container mx-auto max-w-4xl">
        <h1 class="text-3xl font-bold mb-8 text-center text-gray-800">自定义属性演示</h1>
        
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
            <!-- 使用data-*格式的自定义属性 -->
            <div class="product-item bg-white rounded-lg shadow p-6"
                 data-id="101"
                 data-name="笔记本电脑"
                 data-price="5999"
                 data-category="电子产品">
                <h3 class="text-xl font-semibold mb-2">笔记本电脑</h3>
                <p class="text-gray-600 mb-4">高性能商务本，适合办公和设计</p>
                <div class="flex justify-between items-center">
                    <span class="text-blue-600 font-bold">¥5999</span>
                    <button class="view-details bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
                        <i class="fa fa-info-circle mr-1"></i>查看详情
                    </button>
                </div>
            </div>
            
            <div class="product-item bg-white rounded-lg shadow p-6"
                 data-id="203"
                 data-name="机械键盘"
                 data-price="399"
                 data-category="电脑配件">
                <h3 class="text-xl font-semibold mb-2">机械键盘</h3>
                <p class="text-gray-600 mb-4">青轴机械键盘，打字手感极佳</p>
                <div class="flex justify-between items-center">
                    <span class="text-blue-600 font-bold">¥399</span>
                    <button class="view-details bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
                        <i class="fa fa-info-circle mr-1"></i>查看详情
                    </button>
                </div>
            </div>
            
            <div class="product-item bg-white rounded-lg shadow p-6"
                 data-id="305"
                 data-name="无线耳机"
                 data-price="899"
                 data-category="音频设备">
                <h3 class="text-xl font-semibold mb-2">无线耳机</h3>
                <p class="text-gray-600 mb-4">主动降噪，续航可达24小时</p>
                <div class="flex justify-between items-center">
                    <span class="text-blue-600 font-bold">¥899</span>
                    <button class="view-details bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
                        <i class="fa fa-info-circle mr-1"></i>查看详情
                    </button>
                </div>
            </div>
        </div>
        
        <!-- 详情展示区域 -->
        <div id="product-details" class="mt-8 bg-white p-6 rounded-lg shadow hidden">
            <h2 class="text-2xl font-bold mb-4 text-gray-800">产品详情</h2>
            <div id="details-content" class="space-y-2">
                <!-- 这里将通过JavaScript动态填充内容 -->
            </div>
            <button id="close-details" class="mt-4 text-red-500 hover:text-red-700">
                <i class="fa fa-times mr-1"></i>关闭详情
            </button>
        </div>
    </div>

    <script>
        // 等待页面加载完成
        document.addEventListener('DOMContentLoaded', function() {
            // 获取所有"查看详情"按钮
            const detailButtons = document.querySelectorAll('.view-details');
            const detailsSection = document.getElementById('product-details');
            const detailsContent = document.getElementById('details-content');
            const closeButton = document.getElementById('close-details');
            
            // 为每个按钮添加点击事件
            detailButtons.forEach(button => {
                button.addEventListener('click', function() {
                    // 获取按钮所在的产品元素
                    const productItem = this.closest('.product-item');
                    
                    // 使用dataset获取自定义属性值
                    const productId = productItem.dataset.id;
                    const productName = productItem.dataset.name;
                    const productPrice = productItem.dataset.price;
                    const productCategory = productItem.dataset.category;
                    
                    // 动态生成详情内容
                    detailsContent.innerHTML = `
                        <p><strong>ID:</strong> ${productId}</p>
                        <p><strong>名称:</strong> ${productName}</p>
                        <p><strong>价格:</strong> ¥${productPrice}</p>
                        <p><strong>类别:</strong> ${productCategory}</p>
                    `;
                    
                    // 显示详情区域
                    detailsSection.classList.remove('hidden');
                });
            });
            
            // 关闭详情按钮事件
            closeButton.addEventListener('click', function() {
                detailsSection.classList.add('hidden');
            });
        });
    </script>
</body>
</html>
